import json
import socket
from collections import namedtuple

udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind(("127.0.0.1", 9999))
Client = namedtuple("Client", ["peer", "channelId"])
clients: list[Client] = []

while True:
    try:
        c, address = udp_socket.recvfrom(1024)
        _c = c.decode("utf-8")
        if len(list(filter(lambda item: item.peer == address, clients))) <= 0:
            clients += [Client(address, 0)]
        arr = str(_c).split('||')
        header = json.loads(arr[0])
        content = json.loads(arr[1])
        for client in filter(lambda client: client.channelId == int(header['to'][0]['channelId']), clients):
            udp_socket.sendto(c, client.peer)
    except Exception as e:
        print(e)
